﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Sys_Dic_Dic_CodeTable_A : AdminBasePage
{
    decimal codeID = 0;
    public string cat = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[TSCodeTableMetadata.ColumnNames.CodeID] != null)
        {
            codeID = decimal.Parse(Request.QueryString[TSCodeTableMetadata.ColumnNames.CodeID]);
        }
        cat = Request.QueryString["cat"];


        if (!IsPostBack)
        {
            drpCate.SelectedValue = cat;
            switch (ActionType)
            {
                case OperationType.Add:
                    txtStartDate.Value = DateTime.Now.AddDays(-1).Date.ToString(ConstantValue.DateFormat);
                    txtEndate.Value = "9998-12-31";

                    btnSave.Text = "新增";
                    break;
                case OperationType.Update:

                    TSCodeTable code = new TSCodeTable();
                    code.LoadByPrimaryKey(codeID);

                    txtCode.Text = code.Code;
                    txtCode.Enabled = false;
                    txtSeq.Text = code.CodeSequence.ToString();

                    drpCate.SelectedValue = code.CodeCategory;

                    txtValue1.Text = code.Value1;
                    txtValue2.Text = code.Value2;
                    txtStartDate.Value = code.EffectiveStartDate.Value.ToString(ConstantValue.DateFormat);
                    txtEndate.Value = code.EffectiveEndDate.Value.ToString(ConstantValue.DateFormat);

                    btnSave.Text = "修改";
                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        lbMessage.Text = "";
        try
        {
            TSCodeTable code = new TSCodeTable();

            switch (ActionType)
            {
                case OperationType.Add:
                    code.Query.Where(code.Query.Code == txtCode.Text.Trim());
                    code.Query.Load();
                    if (code.es.HasData)
                    {
                        lbMessage.Text = "字典代码已存在！";
                        return;
                    }

                    code.Query.Where(code.Query.Value1 == txtValue1.Text.Trim() && code.Query.CodeCategory == drpCate.SelectedValue);
                    code.Query.Load();
                    if (code.es.HasData)
                    {
                        lbMessage.Text = "字典名称已存在！";
                        return;
                    }
                    code.Code = txtCode.Text;
                    code.Value1 = txtValue1.Text;
                    code.Value2 = txtValue2.Text;
                    code.CodeCategory = drpCate.SelectedValue;
                    code.CodeSequence = txtSeq.IntValue;


                    code.EffectiveEndDate = DateTime.Parse(txtEndate.Value);
                    code.EffectiveStartDate = DateTime.Parse(txtStartDate.Value);
                    code.CreatedBy = code.LastedUpdatedBy = LoginUserInfo.CurrentUser.UserName;
                    code.CreatedDate = code.LastedUpdatedDate = DateTime.Now;
                    code.Save();


                    break;
                case OperationType.Update:

                    code.Query.Where(code.Query.Code == txtCode.Text.Trim() && code.Query.CodeID != codeID);
                    code.Query.Load();
                    if (code.es.HasData)
                    {
                        lbMessage.Text = "字典代码已存！";
                        return;
                    }

                    code.Query.Where(code.Query.Value1 == txtValue1.Text.Trim() && code.Query.CodeCategory == drpCate.SelectedValue && code.Query.CodeID != codeID);
                    code.Query.Load();
                    if (code.es.HasData)
                    {
                        lbMessage.Text = "字典名称已存在！";
                        return;
                    }

                    code.LoadByPrimaryKey(codeID);


                    code.Value1 = txtValue1.Text;
                    code.Value2 = txtValue2.Text;
                    code.CodeSequence = txtSeq.IntValue;
                    code.EffectiveEndDate = DateTime.Parse(txtEndate.Value);
                    code.EffectiveStartDate = DateTime.Parse(txtStartDate.Value);
                    code.CreatedBy = code.LastedUpdatedBy = LoginUserInfo.CurrentUser.UserName;
                    code.CreatedDate = code.LastedUpdatedDate = DateTime.Now;

                    code.LastedUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    code.LastedUpdatedDate = DateTime.Now;
                    code.Save();
                    break;
                case OperationType.Delete:
                    code.LoadByPrimaryKey(codeID);
                    code.Save();
                    WebHelper.GotoParent();
                    break;
            }
            WebHelper.CloseDialog(true, new Dictionary<string, string>() { { "cat", drpCate.SelectedValue } });
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }

    }
}